package com.pocketsweet.ui.uilib;

import java.util.Timer;
import java.util.TimerTask;

import com.pocketsweet.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class myTextViewShow extends TextView {
	int textIndex = 0;
	String text;
	String subText;
	Handler h;
	TimerTask task;
	Timer timer;

	public myTextViewShow(Context context) {
		super(context);
		text = "";
	}

	public myTextViewShow(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray array = context.obtainStyledAttributes(attrs,
				R.styleable.TextViewShow);
		text = array.getString(R.styleable.TextViewShow_textShow);
		array.recycle();
		h = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					setText(subText);
					;
					break;
				}
			}
		};
		task = new TimerTask() {
			public void run() {
				if (textIndex != text.length()) {
					subText = text.substring(0, textIndex);
					textIndex++;
				} else {
					subText = text.substring(0, textIndex);
				}
				Message msg = new Message();
				msg.what = 0;
				h.sendMessage(msg);

			}
		};
		timer = new Timer();
	}

	public void startShow() {
		timer.schedule(task, 0, 200);
	}

	public String getTextString() {
		return this.text;
	}

	public myTextViewShow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		text = "";
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}
}
